/**
 * 
 */
package de.akabahn.ebd.dbd;

import de.akabahn.ebd.dbd.impl.DBDSignalD;
import de.akabahn.ebd.dbd.impl.DBDSignalF;

/**
 * Common class that represents a signal in the DBD server and that provides
 * constants and method to define and manipulate signal aspects.
 * 
 * @author Peter Eimann
 * @since 0.2
 * @version 1.0
 * @see DBDSignalD
 * @see DBDSignalF
 */
public abstract class DBDSignal {

	public static final int BIT0 = (int) Math.pow(2, 0);
	public static final int BIT1 = (int) Math.pow(2, 1);
	public static final int BIT2 = (int) Math.pow(2, 2);
	public static final int BIT3 = (int) Math.pow(2, 3);
	public static final int BIT4 = (int) Math.pow(2, 4);
	public static final int BIT5 = (int) Math.pow(2, 5);
	public static final int BIT6 = (int) Math.pow(2, 6);
	public static final int BIT7 = (int) Math.pow(2, 7);
	public static final int BIT8 = (int) Math.pow(2, 8);
	public static final int BIT9 = (int) Math.pow(2, 9);
	public static final int BIT10 = (int) Math.pow(2, 10);
	public static final int BIT11 = (int) Math.pow(2, 11);
	public static final int BIT12 = (int) Math.pow(2, 12);
	public static final int BIT13 = (int) Math.pow(2, 13);
	public static final int BIT14 = (int) Math.pow(2, 14);
	public static final int BIT15 = (int) Math.pow(2, 15);
	public static final int BIT16 = (int) Math.pow(2, 16);
	public static final int BIT17 = (int) Math.pow(2, 17);
	public static final int BIT18 = (int) Math.pow(2, 18);
	public static final int BIT19 = (int) Math.pow(2, 19);
	public static final int BIT20 = (int) Math.pow(2, 20);
	public static final int BIT21 = (int) Math.pow(2, 21);
	public static final int BIT22 = (int) Math.pow(2, 22);
	public static final int BIT23 = (int) Math.pow(2, 23);
	public static final int BIT24 = (int) Math.pow(2, 24);
	public static final int BIT25 = (int) Math.pow(2, 25);
	public static final int BIT26 = (int) Math.pow(2, 26);
	public static final int BIT27 = (int) Math.pow(2, 27);
	public static final int BIT28 = (int) Math.pow(2, 28);
	public static final int BIT29 = (int) Math.pow(2, 29);

	protected int intValue;

	/**
	 * Return the current integer value that defines the signal aspect
	 * 
	 * @return the intValue The value of the signal aspect
	 */
	public int getIntValue() {
		return intValue;
	}

	/**
	 * Set the integer value of the current signal aspect. It may be defined by
	 * using the provided constants.
	 * 
	 * @param intValue
	 *            the signal aspect to set
	 */
	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}

	/**
	 * Set the signal aspects that correspond to the passed
	 * <code>intValues</code>.
	 * 
	 * @param intValues
	 *            The signal aspects
	 */
	public void setIntValues(int... intValues) {
		for (int intValue : intValues) {
			setBit(intValue);
		}
	}

	/**
	 * Set the passed <code>bit</code>, for convenience the provided constants
	 * should be used.
	 * 
	 * @param bit
	 *            The bit to set
	 */
	public void setBit(int bit) {
		this.intValue |= bit;
	}

	/**
	 * Clear the passed <code>bit</code>, for convenience the provided constants
	 * should be used.
	 * 
	 * @param bit
	 *            The bit to clear
	 */
	public void removeBit(int bit) {
		this.intValue &= ~bit;
	}

	/**
	 * Print the binary representation of the current signal aspect value.
	 * 
	 * @return The binary representation, the rightmost bit is bit 0, the
	 *         leftmost bit 29.
	 */
	public String toBinaryString() {
		StringBuilder buf = new StringBuilder("000000000000000000000000000000");
		String bin = Integer.toBinaryString(this.intValue);
		buf.replace(30 - bin.length(), 30, bin);
		return buf.toString();
	}
}
